/*
 * @(#) MessageUtil.java 2010-9-26
 *
 * Copyright (c) 2010, Strong Technology. All Rights Reserved.
 * Strong Technology. CONFIDENTIAL
 */
package cn.strong.gweva.common.util;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
 * 消息工具类
 * 
 * @author lilanfei
 * @version 1.0
 * @since 2010-9-26
 */
public class MessageUtil implements MessageCode
{

	// LOG
	private static Log log = LogFactory.getFactory().getInstance(MessageUtil.class);

	/**
	 * private constructor to enhance the singleton design pattern
	 */
	private MessageUtil()
	{
		// empty constructor
	}

	// /////////////////////////////////////////////
	// properties

	private static final String PROPERTIES_FILE_NAME = "messages";

	private static final Map<String, String> MESSAGE_MAP;

	// /////////////////////////////////////////////
	// static

	static
	{
		MESSAGE_MAP = new HashMap<String, String>();

		try
		{
			ResourceBundle rb = ResourceBundle.getBundle(PROPERTIES_FILE_NAME);
			Enumeration<String> enum1 = rb.getKeys();

			while (enum1.hasMoreElements())
			{
				String key = enum1.nextElement();
				String msg = rb.getString(key);
				MESSAGE_MAP.put(key, msg);
			}

		}
		catch (Exception e)
		{
			log.error("ExceptionMsgUtil.static error : " + e);
		}
	}

	// /////////////////////////////////////////////
	// public static method
	/**
     */
	public static boolean isFind(String key)
	{
		if (key == null || key.trim().length() == 0)
		{
			return false;
		}

		return MESSAGE_MAP.keySet().contains(key.trim());
	}

	/**
     */
	public static String getMsg(String key)
	{
		if (!isFind(key))
		{
			return null;
		}

		return MESSAGE_MAP.get(key.trim());
	}

	/**
     */
	public static String getMsg(String key, String replacement)
	{
		if (!isFind(key))
		{
			return null;
		}
		if (replacement == null)
		{
			return MESSAGE_MAP.get(key.trim());
		}

		return StringUtil.setReplacement4Msg(MESSAGE_MAP.get(key.trim()),
				new String[] { replacement });
	}

	/**
     */
	public static String getMsg(String key, String replacement1, String replacement2)
	{
		if (!isFind(key))
		{
			return null;
		}
		if (replacement1 == null || replacement2 == null)
		{
			return MESSAGE_MAP.get(key.trim());
		}

		return StringUtil.setReplacement4Msg(MESSAGE_MAP.get(key.trim()), new String[] {
				replacement1, replacement2 });
	}

	/**
     */
	public static String getMsg(String key, String replacement1, String replacement2,
			String replacement3)
	{
		if (!isFind(key))
		{
			return null;
		}
		if (replacement1 == null && replacement2 == null && replacement3 == null)
		{
			return MESSAGE_MAP.get(key.trim());
		}

		return StringUtil.setReplacement4Msg(MESSAGE_MAP.get(key.trim()), new String[] {
				replacement1, replacement2, replacement3 });
	}

	/**
     */
	public static String getMsg(String key, String[] replacementArray)
	{
		if (!isFind(key))
		{
			return null;
		}
		if (replacementArray == null || replacementArray.length == 0)
		{
			return MESSAGE_MAP.get(key.trim());
		}
		return StringUtil.setReplacement4Msg(MESSAGE_MAP.get(key.trim()), replacementArray);
	}

	/**
     */
	public static String getMsg(String key, Object[] replacementArray)
	{
		if (!isFind(key))
		{
			return null;
		}
		if (replacementArray == null || replacementArray.length == 0)
		{
			return MESSAGE_MAP.get(key.trim());
		}
		String[] strReplace = new String[replacementArray.length];
		for (int i = 0; i < replacementArray.length; i++)
		{
			strReplace[i] = replacementArray[i].toString();
		}
		return StringUtil.setReplacement4Msg(MESSAGE_MAP.get(key.trim()), strReplace);
	}
}
